package com.zhxh.nzpos.setting;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.Constants;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.GoodsDao;
import com.zhxh.nzpos.goods.GoodsItem;
import com.zhxh.nzpos.model.Goods;
import com.zhxh.nzpos.setting.PriceSettingAdapter.ViewHolder;
import com.zhxh.nzpos.utils.ViewUtil;

/**
 * 价格设定界面
 * 
 * @author wangzt
 *
 */
public class PriceSettingActivity extends BaseActivity implements ItemActionCallback {
	private ArrayList<GoodsItem> goodsItems;
	private PriceSettingAdapter listAdapter;
	GoodsDao goodsDao;
	private int position;
	
	 EditText dealOcr;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_price);
        goodsDao = new GoodsDao(this);
        initViews();
    }
	
	private void initViews() {
		ListView listView = (ListView) findViewById(R.id.price_listview);
		goodsItems = new ArrayList<GoodsItem>();
		
		// TODO 扫描条码添加一项
		dealOcr = (EditText) findViewById(R.id.goods_ocr_et);
	
		Button scanBtn = (Button) findViewById(R.id.buyin_scan_btn);
		scanBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Constants.SCAN_ACTION);
				startActivityForResult(intent, Constants.SCAN_REQUEST_CODE);
			}
		});
		
		listAdapter = new PriceSettingAdapter(getLayoutInflater(), goodsItems, this);
		listView.setAdapter(listAdapter);
		
/*		Button cancelBtn = (Button) findViewById(R.id.price_cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});*/
	}

	public void onDelete(int pos) {
		goodsItems.remove(pos);
		listAdapter.notifyDataSetChanged();
	}

	public void search(View view) {
		final EditText dealOcr = (EditText) findViewById(R.id.goods_ocr_et);
		Editable ocr = dealOcr.getText();
		if (ocr!= null) {
			String ocrStr = ocr.toString();
			if(goodsItems == null) {
				goodsItems = new ArrayList<GoodsItem>();
			}
			else {
				goodsItems.clear();
				
			}
			GoodsDao dao = new GoodsDao(this);
			Goods goods = dao.find(ocrStr.trim());
			if(goods != null) {
				GoodsItem item = copyProperties (goods);
		        goodsItems.add(item);
					if (listAdapter != null) {
						listAdapter.notifyDataSetChanged();
					}
			}
			else {
				ViewUtil.makeToast(this, "没有找到相关商品");
			}
		}
	}
	
	
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		switch (resultCode){  
        case RESULT_OK:  
        	if (requestCode == Constants.SCAN_REQUEST_CODE) {
    			if (resultCode == RESULT_OK) {
    				String ocrStr = data.getStringExtra(Constants.SCAN_RESULT_EXTRA);
    				if (!TextUtils.isEmpty(ocrStr)) {
    					dealOcr.setText(ocrStr);
    				} else {
    					Toast.makeText(getApplicationContext(), "请重新扫描！",
    							Toast.LENGTH_LONG).show();
    				}

    			} else if (resultCode == RESULT_CANCELED) {
    				Toast.makeText(getApplicationContext(), "请重新扫描！",
    						Toast.LENGTH_LONG).show();
    			}
    	
    			}

           
        }  

	}


	private GoodsItem copyProperties (Goods goods) {
		GoodsItem item = new GoodsItem();
		item.setId(goods.getId());
		item.setCategoryId(goods.getCategoryId());
		item.setCategoryName(goods.getCategoryName());
		item.setName(goods.getName());
		item.setNote(goods.getNote());
		item.setPrice1(goods.getPrice1());
		item.setPrice2(goods.getPrice2());
		item.setSpecification(goods.getSpecification());
		item.setUnits1(goods.getUnits1());
		item.setUnits2(goods.getUnits2());
		item.setPoints(goods.getPonits());
		item.setCode(goods.getCode());
		return item;
	}
	
	private Goods copyProperties (GoodsItem goods) {
		Goods item = new Goods();
		item.setId(goods.getId());
		item.setCategoryId(goods.getCategoryId());
		item.setCategoryName(goods.getCategoryName());
		item.setName(goods.getName());
		item.setNote(goods.getNote());
		item.setPrice1(goods.getPrice1());
		item.setPrice2(goods.getPrice2());
		item.setSpecification(goods.getSpecification());
		item.setUnits1(goods.getUnits1());
		item.setUnits2(goods.getUnits2());
		item.setCode(goods.getCode());
		item.setPonits(goods.getPoints());
		return item;
	}

	@Override
	public void onUpdate(int position, ViewHolder holder) {
		GoodsItem item = goodsItems.get(position);
		 Goods goods = copyProperties (item);
		
		if(holder.price.getText().toString().length() == 0) {
			holder.price.setError("价格不能为空");
			return;
		}
		if(holder.price2.getText().toString().length() == 0) {
			holder.price2.setError("价格不能为空");
			return;
		}
		if(holder.points.getText().toString().length() == 0) {
			holder.points.setError("积分不能为空");
			return;
		}
		if(holder.unit2.getText().toString().length() == 0) {
			holder.unit2.setError("积分不能为空");
			return;
		}
		 goods.setPrice1(Float.parseFloat(holder.price.getText().toString()));
		 goods.setPrice2(Float.parseFloat(holder.price2.getText().toString()));
		 goods.setPonits(Integer.parseInt(holder.points.getText().toString()));
		 goods.setUnits2(holder.unit2.getText().toString());
		int count =  goodsDao.update(goods);
		if(count > 0)
		ViewUtil.makeToast(this, "信息保存成功!");
		else {
			ViewUtil.makeToast(this, "信息保存失败，请检查信息录入格式是否正确!");
		}
		//listAdapter.notifyDataSetChanged();
		
	}
	
}
